//passive_tcp.c
//create a passive TCP socket and return sockfd

#include "passive_tcp.h"
#include "chatter_err_log.h"

int passiveTCP(const int port,const int qlen)
{
	struct	sockaddr_in sin;		/* an Internet endpoint address       */
	int		server_sockfd;			/* server's socket file discripter */

	memset(&sin, 0, sizeof(sin));

	sin.sin_family		=	AF_INET;
	sin.sin_addr.s_addr =	INADDR_ANY;
	sin.sin_port		=	htons(port);

	server_sockfd = socket(PF_INET, SOCK_STREAM , 0);

	if (server_sockfd == -1)
		err_exit("can't create socket %d: %m\n", server_sockfd);

	/* Bind the socket */
	if (bind(server_sockfd, (struct sockaddr *)&sin, sizeof(sin)) == -1)
		err_exit("can't bind to port_%d: %m\n", port);	

	if (listen(server_sockfd, qlen) == -1)
		err_exit("can't listen on port_%d : %m\n", port);
	return server_sockfd;
}
